package com.zyf.service.file.impl;

import cn.hutool.core.util.ObjectUtil;
import com.zyf.service.file.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Service
public class FileServiceImpl implements FileService {
    @Override
    public void upload(MultipartFile file) {
        if (ObjectUtil.isEmpty(file)) {
            return;
        }
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        // 字节流
//        try {
//            // 获取文件字节输入流
//            InputStream inputStream = file.getInputStream();
//            OutputStream outputStream = new FileOutputStream("C:\\Users\\admin\\Desktop\\upload-demo\\test-" + originalFilename);
//            int len;
//            byte[] buffer = new byte[1024];
//            while ((len = inputStream.read(buffer)) != -1) {
//                outputStream.write(buffer, 0, len);
//            }
//            inputStream.close();
//            outputStream.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        // 字符流
//        try {
//            InputStream inputStream = file.getInputStream();
//            Reader reader = new InputStreamReader(inputStream);
//            Writer writer = new FileWriter("C:\\Users\\admin\\Desktop\\upload-demo\\" + originalFilename);
//            int len;
//            char[] buffer = new char[1];
//            while ((len = reader.read(buffer)) != -1) {
//                writer.write(buffer, 0, len);
//            }
//            writer.flush();
//            reader.close();
//            writer.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        File file1 = new File("/usr/local/upload-demo",  originalFilename);
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }

//        /usr/local/upload-demo
    }

    @Override
    public void fragmentUpload(MultipartFile file) {
        
    }
}
